﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.UC.客户CRM.Service;
using DXApplicationTangche.UC.下单MTO;
using DevExpress.XtraGrid.Demos.util;
using System.Text.RegularExpressions;
using DXApplicationTangche.UC.订单一览新.销售确认单;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.Utils.Win;
using mendian;
using DXApplicationTangche.UC.销售记录.service;
using System.IO;

namespace DXApplicationTangche.UC.订单一览新.客户充值
{
    public partial class Form会员预存无免单维护 : DevExpress.XtraEditors.XtraForm
    {
        private RechargeNoneVerifyVO rechargeVerifyVO = new RechargeNoneVerifyVO();
        private CustomerPDTO customerPDTO = new CustomerPDTO();
        private String mobile { get; set; }
        public Form会员预存无免单维护()
        {
            InitializeComponent();
            this.ActiveControl = this.textBox手机号查找0;
            this.searchLookUpEdit店铺.Properties.DataSource = 销售记录Service.get店铺();
            this.searchLookUpEdit销售员.Properties.DataSource = 销售记录Service.get销售员();
            this.config读取店铺();
        }
        
        #region 下单选店铺
        private void searchLookUpEdit店铺_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear店铺Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit店铺View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit店铺.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String storeId = this.searchLookUpEdit店铺.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    String storeName = this.searchLookUpEdit店铺.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    this.rechargeVerifyVO.buildStore(storeId, storeName);
                    if (!System.IO.File.Exists(@"xml\config店铺.xml"))
                    {
                        DealXML.ObjectToXMLFile(new DTO销售表头(this.rechargeVerifyVO.storeId, this.rechargeVerifyVO.storeName), @"xml\config店铺.xml", Encoding.UTF8);
                        this.config读取店铺();
                    }
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear店铺Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit店铺.ToolTip = null;
            searchLookUpEdit店铺.EditValue = null;
        }
        private void searchLookUpEdit店铺_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.rechargeVerifyVO.storeName;
        }
        private void config读取店铺()
        {
            if (System.IO.File.Exists(@"xml\config店铺.xml"))
            {
                DTO销售表头 dTO店铺 = DealXML.XMLFlieToObject<DTO销售表头>(@"xml\config店铺.xml", Encoding.UTF8);
                this.rechargeVerifyVO.buildStore(dTO店铺.id, dTO店铺.name);
                this.searchLookUpEdit店铺.Text = this.rechargeVerifyVO.storeName;
                this.searchLookUpEdit店铺.Enabled = false;
            }
        }
        private void simpleButton重选店铺_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(@"xml\config店铺.xml"))
            {
                File.Delete(@"xml\config店铺.xml");
                this.searchLookUpEdit店铺.Enabled = true;
                this.rechargeVerifyVO.clearStore();
                this.searchLookUpEdit店铺.Text = "";
            }
        }
        #endregion
        #region 下单选销售员
        private void searchLookUpEdit销售员_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear销售员Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit销售员View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit销售员.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String assistantId = this.searchLookUpEdit销售员.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    String assistantName = this.searchLookUpEdit销售员.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    this.rechargeVerifyVO.buildAssistant(assistantId, assistantName);
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear销售员Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit销售员.ToolTip = null;
            searchLookUpEdit销售员.EditValue = null;
        }
        private void searchLookUpEdit销售员_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value) { }
            e.DisplayText = this.rechargeVerifyVO.assistantName;
        }
        #endregion

        private void simpleButton确认_Click_1(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.mobile))
            {
                MessageBox.Show("请先选择预存客户");
                return;
            }
            if (!this.rechargeVerifyVO.build判空())
            {
                MessageBox.Show("请选择店铺、销售员");
                return;
            }
            //  新增
            会员预存Service.saveMemberRecharge(
                new MemberRechargeRequestDTO(
                    this.mobile, 
                    0.0,
                    this.textBox充值金额.Text.Trim(), 
                    this.rechargeVerifyVO.storeId,
                    this.rechargeVerifyVO.assistantId
                    )
                );
            this.Close();
        }

        private void textBox手机号查找0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                this.mobile = this.textBox手机号查找0.Text.Trim();
                this.customerPDTO = MTO订单Service.getCustomerPByMobile(this.textBox手机号查找0.Text.Trim());
                if (String.IsNullOrEmpty(this.customerPDTO.mobile))
                {
                    MessageBox.Show("未创建用户");
                    return;
                }
                this.手机号查找信息并赋值(this.customerPDTO.mobile);
                this.textBox手机号查找0.Clear();
                this.ActiveControl = this.textBox充值金额;
            }
        }
        private void simpleButton更多客户_Click(object sender, EventArgs e)
        {
            new FormMTO客户一览(this.customerPDTO).ShowDialog();
            this.手机号查找信息并赋值(this.customerPDTO.mobile);
            this.textBox手机号查找0.Clear();
        }
        private void simpleButton添加客户0_Click(object sender, EventArgs e)
        {
            new FormMTO添加客户地址(this.customerPDTO).ShowDialog();
            this.手机号查找信息并赋值(this.customerPDTO.mobile);
            this.textBox手机号查找0.Clear();
        }
        private void 手机号查找信息并赋值(String getMobile)
        {
            this.mobile = getMobile;
            //根据电话号实例化对象并给textbox赋值
            this.customerPDTO = CRM客户Service.getCustomerPByMobile(getMobile);
            this.textBox客户姓名.Text = this.customerPDTO.customerName;
            this.textBox联系电话.Text = this.customerPDTO.mobile;
            this.textBox详细地址.Text =
                this.customerPDTO.addressProvince
                + this.customerPDTO.addressCity
                + this.customerPDTO.addressDistrict
                + this.customerPDTO.addressDetail;
        }

        private void textBox折让金额_Validating(object sender, CancelEventArgs e)
        {
            if (!FunctionHelper.Is大于0(this.textBox充值金额.Text.Trim()))
            {
                MessageBox.Show("充值金额必须大于0！");
                this.ActiveControl = this.textBox充值金额;
                return;
            }
        }
    }

    public class RechargeNoneVerifyVO
    {
        public String mobile { get; set; }
        public String storeId { get; set; }
        public String storeName { get; set; }
        public String assistantId { get; set; }
        public String assistantName { get; set; }
        public RechargeNoneVerifyVO()
        {

        }
        public RechargeNoneVerifyVO buildMobile(String mobile)
        {
            this.mobile = mobile;
            return this;
        }
        public RechargeNoneVerifyVO buildStore(String storeId, String storeName)
        {
            this.storeId = storeId;
            this.storeName = storeName;
            return this;
        }

        public RechargeNoneVerifyVO buildAssistant(String assistantId, String assistantName)
        {
            this.assistantId = assistantId;
            this.assistantName = assistantName;
            return this;
        }
        public Boolean build判空()
        {
            if (String.IsNullOrEmpty(this.storeId)
                || String.IsNullOrEmpty(this.assistantId))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public void clearStore()
        {
            this.storeId = "";
            this.storeName = "";
        }
    }
}